<?php

    namespace app\store\admin;

    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\member\model\MemberModel;
    use app\payment\logic\PayBase;
    use app\store\model\AddressModel;
    use app\store\model\OrderInfoModel;
    use app\store\model\OrderModel;

    class Order extends Admin
    {

        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function lists($status = null)
        {

            // 查询
            $map = $this->getMap();
            // 排序
            $order = $this->getOrder('id desc');
            if ($status !== null) {
                $map['status'] = $status;
            }
            $data_list = OrderModel::where($map)->where('status < 90')->order($order)->paginate();
            // 字段管理按钮
            $RightButtons = [
                'custom' => [
                    'title' => '详情',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('send', ['order_sn' => '__order_sn__'])
                ]
            ];
            $TopButtons = [
                'status' => [
                    'title' => '所有',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('order/lists')
                ],
                'status0' => [
                    'title' => '已取消',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('order/lists', ['status' => OrderModel::STATUS_CANCEL])
                ],
                'status10' => [
                    'title' => '未付款',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('order/lists', ['status' => OrderModel::STATUS_NO_PAY])
                ],
                'status20' => [
                    'title' => '已付款',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('order/lists', ['status' => OrderModel::STATUS_YES_PAY])
                ],
//                'status21' => [
//                    'title' => '退款审核',
//                    'icon' => 'fa fa-fw fa-navicon',
//                    'href' => url('order/lists', ['status' => OrderModel::STATUS_USER_REFUND])
//                ],
//                'status22' => [
//                    'title' => '退款成功',
//                    'icon' => 'fa fa-fw fa-navicon',
//                    'href' => url('order/lists', ['status' => OrderModel::STATUS_YES_REFUND])
//                ],
//                'status23' => [
//                    'title' => '退款失败',
//                    'icon' => 'fa fa-fw fa-navicon',
//                    'href' => url('order/lists', ['status' => OrderModel::STATUS_NO_REFUND])
//                ],
                'status30' => [
                    'title' => '已发货',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('order/lists', ['status' => OrderModel::STATUS_DELIVER])
                ],
                'status40' => [
                    'title' => '已收货',
                    'icon' => 'fa fa-fw fa-navicon',
                    'href' => url('order/lists', ['status' => OrderModel::STATUS_DELIVER_SIGN])
                ],
//                'status41' => [
//                    'title' => '退货',
//                    'icon' => 'fa fa-fw fa-navicon',
//                    'href' => url('order/lists', ['status' => OrderModel::STATUS_DELIVER_REFUND])
//                ],
//                'status42' => [
//                    'title' => '退货成功',
//                    'icon' => 'fa fa-fw fa-navicon',
//                    'href' => url('order/lists', ['status' => OrderModel::STATUS_DELIVER_REFUND_YES])
//                ],
//                'status43' => [
//                    'title' => '退货失败',
//                    'icon' => 'fa fa-fw fa-navicon',
//                    'href' => url('order/lists', ['status' => OrderModel::STATUS_DELIVER_REFUND_NO])
//                ]
            ];

            return ZBuilder::make('table')->setPageTitle('订单管理')->setSearch(['order_sn' => '订单编号'])// 设置搜索框
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['order_sn', '订单编号', 'text'],
                ['pay_sn', '支付订单编号', 'text'],
                ['pay_type', '支付方式', 'text', '', PayBase::$PAY_TYPE],
                ['buyer_id', '买方', 'text', '', MemberModel::GetIDsUrl($data_list, 'buyer_id')],
                ['address', '地区', 'callback', function ($value) {
                    return json_decode($value,true)['addr'];
                }],
                ['amount', '订单总价', 'text'],
                ['status', '订单状态', 'text', '', OrderModel::STATUS_VIEW],
                ['right_button', '操作', 'btn']
            ])->addTopButtons($TopButtons)// 批量添加顶部按钮
            ->addRightButtons($RightButtons)// 批量添加右侧按钮
            ->replaceRightButton([
                'order_status' => [
                    'neq',
                    21
                ]
            ], '', 'custom')->addOrder('id,title')->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染模板
        }

        /**
         * 订单详情
         * @param null $order_sn
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function send($order_sn = null)
        {
            $order_sn ? $order_sn : $this->error('参数错误');
            $order = OrderModel::where('order_sn', $order_sn)->find();
            $order['status'] = OrderModel::STATUS_VIEW[$order['status']];
            $order_info = OrderInfoModel::where('order_sn', $order_sn)->select();
            foreach ($order_info as &$item) {
                $item['goods_image'] = get_file_path($item['goods_image']);
            }
            unset($item);

            $this->assign('type', false);
            $this->assign('order', $order);
            $this->assign('order_info', $order_info);
            $address=json_decode($order['address'],true);
            $this->assign('address', $address);

            return $this->fetch();
        }

        /**
         * 退款通过
         * @param $order_sn
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function refund_yes($order_sn)
        {
            $order_sn ? $order_sn : $this->error('参数错误');
            $order = OrderModel::where(['order_sn' => $order_sn])->find();
            if ($order) {

            } else {
                $this->error('订单不存在');
            }
        }

        /**
         * @param $order_sn
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function refund_no($order_sn)
        {
            $order_sn ? $order_sn : $this->error('参数错误');
            $order = OrderModel::where(['order_sn' => $order_sn])->find();
            if ($order) {

            } else {
                $this->error('订单不存在');
            }
        }
    }